package com.dinzeer.slashblade_useful_addon.mixin;

import com.dinzeer.slashblade_useful_addon.config.Config;
import mods.flammpfeil.slashblade.capability.slashblade.ISlashBladeState;
import mods.flammpfeil.slashblade.entity.EntityAbstractSummonedSword;
import mods.flammpfeil.slashblade.entity.Projectile;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(EntityAbstractSummonedSword.class)
public class AbsSummonSwordMixin extends Projectile {





    @Shadow(remap = false)
    private double damage;

    protected AbsSummonSwordMixin(EntityType<? extends net.minecraft.world.entity.projectile.Projectile> p_37248_, Level p_37249_) {
        super(p_37248_, p_37249_);
    }


    @Override
    public boolean isOnFire() {

        return false;
    }

    @Inject(method = "setDamage", at = @At("HEAD"), cancellable = true, remap = false)
    private void onSetDamage(double damageIn, CallbackInfo ci) {
        EntityAbstractSummonedSword sword = (EntityAbstractSummonedSword)(Object)this;
        LivingEntity owner = (LivingEntity) sword.getOwner();

        if (owner instanceof Player player) {
            float extra = 1.0f;

            extra += (float) (slashblade_useful_addon$getRefined(player) * Config.refinedFactor);
            extra += (float) (slashblade_useful_addon$getBaseAttackModifier(player) *  Config.attackModifierFactor);

            damageIn = (int)(damageIn * extra);
          this.damage= damageIn;
          ci.cancel();
        }
    }

    @Unique
    private static int slashblade_useful_addon$getRefined(Player player){
        if(player.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE)
                .map(ISlashBladeState::getRefine).isPresent()){
        return player.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE)
                .map(ISlashBladeState::getRefine).get();
        }
        return 0;
    }

    @Unique
    private static Float slashblade_useful_addon$getBaseAttackModifier(Player player){
        if(player.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE)
                .map(ISlashBladeState::getBaseAttackModifier).isPresent()){
        return player.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE)
                .map(ISlashBladeState::getBaseAttackModifier).get();
        }
        return 0f;
    }
}
